package com.lxp;

import com.alibaba.fastjson.JSON;
import com.lxp.common.model.TbConnector;
import com.lxp.common.model.TbGateWay;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@SpringBootApplication
public class MqttDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MqttDemoApplication.class, args);
    }

    @SneakyThrows
    @PostConstruct
    public static void getJson() {
        List<TbGateWay> gateWayList = new ArrayList<>();

        List<TbConnector> connectorList = new ArrayList<>();
        TbConnector connector = new TbConnector("modbus", "Modbus Connector", "modbus.json");
        connectorList.add(connector);

        String setupFloder = "/thingsboard/config";
        String configSettingsFile = setupFloder + File.separator + "tb_gateway.json";

        TbGateWay context = new TbGateWay();
        context.setHost("192.168.1.250");
        context.setAccessToken("111");
        context.setConnectors(connectorList);
        gateWayList.add(context);

        String jsonString = JSON.toJSONString(gateWayList, true);
        Path path = Paths.get(configSettingsFile);
        Files.write(path, jsonString.getBytes(StandardCharsets.UTF_8));


    /*    byte[] bytes = Files.readAllBytes(Paths.get(configSettingsFile));
        SystemContext context = JSON.parseObject(bytes, SystemContext.class);
        log.info("CameraContext:\r\n{}", JSON.toJSONString(context, true));*/
    }
}
